package com.sdp.wz.handle;

import java.util.concurrent.TimeUnit;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sdp.core.bizc.exception.ExceptionMessage;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.wz.utils.WzConstants;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public abstract class WzInOutPutHandle {

	@Autowired
	private RedissonClient redissonClient;
	
	
	public BusinessContext handle(BusinessContext businessContext) {
		RLock lock = redissonClient.getLock(WzConstants.物资出入库REDIS锁名.getCode());
		try {
			if (lock.tryLock(3, TimeUnit.SECONDS)) {
				businessContext = execute(businessContext);
			}else {
				businessContext.setMessage("系统繁忙，请稍后再试");
				businessContext.setSuccess(false);
			}
		} catch (InterruptedException e) {
			log.error(ExceptionMessage.getExceptionMessage(e));
			throw new RuntimeException(e);
		} finally {
			lock.unlock();
		}
		return businessContext;
	}

	public abstract BusinessContext execute(BusinessContext businessContext);
}
